from __future__ import unicode_literals
from threading import Timer
import requests
from wxpy import *

# on windows
bot = Bot()

# under linue environment
bot = Bot(console_qr=2, cache_path="botoo.pkl")

def get_news():
    """获取金山词霸每日一句，英文及其翻译"""
    url = "http://open.iciba.com/dsapi"
    r = requests.get(url)
    content = r.json()["content"]
    note = r.json()["note"]
    return content, note

def send_news():
    try:
        contents = get_news()
        # 好友的微信名(昵)称，不是备注， 也不是微信账号
        my_friend = bot.friends().search("我们都是有梦想的人", sex=MALE)[0]
        my_friend.send(contents[0])
        my_friend.send(contents[1])
        my_friend.send("Have a good one!")
        # 每86400秒（1天），发送一次
        t = Timer(86400, send_news)
        t.start()
    except Exception as e:
        # 昵称不在好友列表
        my_friend = bot.friends().search("Andty")[0]
        my_friend.send("今天消息发送失败了")

import logging

if __name__ == "__main__":
    send_news()
